package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class LoginDemo {
    public static void main(String[] args) {
        try (
                Connection conn = DBUtil.getConnection();
                ){
            System.out.println("欢迎登录");
            Scanner sc=new Scanner(System.in);
            Statement stmt = conn.createStatement();
            String sql = "SELECT username, password FROM userinfo";
            ResultSet rs = stmt.executeQuery(sql);
            boolean flag=false;
            while (true) {
                System.out.println("请输入用户名");
                String username=sc.nextLine();
                System.out.println("请输入密码");
                String password=sc.nextLine();
                while (rs.next()) {
                    String getUsername=rs.getString("username");
                    String gtePassword = rs.getString("password");
                    if(getUsername.equals(username) && gtePassword.equals(password)){
                        flag=true;
                        break;
                    }
                }

                if(flag){
                    System.out.println("登录成功");
                    break;
                }else {
                    System.out.println("登录失败");
                    System.out.println("请重新输入");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
